import domain.Order;
import domain.OrderItem;
import mapper.OrderMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class OrderText {
    private static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        // 1. 定义配置文件的位置
        String resource = "mybatis-config.xml";
        // 2. 获取这个文件的输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 3. 基于配置文件构建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "dev");
    }

    @Test
    public void insert() {
//        数据构建
        Order order = new Order();
        order.setPayment(new BigDecimal(19));
        order.setPaymentType(1);

        List<OrderItem> orderItems = new ArrayList<OrderItem>();
        orderItems.add(new OrderItem("鼠标", new BigDecimal(5), 2));
        orderItems.add(new OrderItem("键盘", new BigDecimal(9), 1));
        order.setOrderItems(orderItems);
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
            orderMapper.insertOrder(order);
            for (OrderItem orderItem : order.getOrderItems()) {
                orderItem.setOrderId(order.getId());
                orderMapper.insertOrderItem(orderItem);
            }
            sqlSession.commit();
        }
    }

    //    批量插入
    @Test
    public void batchInsert() {
        Order order = new Order();
        order.setPayment(new BigDecimal(50));
        order.setPaymentType(1);
        List<OrderItem> list = new ArrayList<>();
        list.add(new OrderItem(
                "鼠标", new BigDecimal(5), 2
        ));
        list.add(new OrderItem(
                "键盘", new BigDecimal(5), 2
        ));
        list.add(new OrderItem(
                "马桶", new BigDecimal(5), 2
        ));
        list.add(new OrderItem(
                "老鼠", new BigDecimal(5), 2
        ));
        list.add(new OrderItem(
                "老猫", new BigDecimal(5), 2
        ));
        order.setOrderItems(list);
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
            orderMapper.insertOrder(order);
            for (OrderItem orderItem : order.getOrderItems()) {
                orderItem.setOrderId(order.getId());
            }
            orderMapper.batchInsertOrderItems(list);
            sqlSession.commit();
        }
    }

    @Test
    public void batchDel() {
        String ids = "14,15,16";
        String[] idss = ids.split(",");
        List<Integer> list = new ArrayList<>();
        for (String id : idss) {
            list.add(Integer.valueOf(id));
        }
        try (SqlSession session = sqlSessionFactory.openSession()) {
            OrderMapper orderMapper = session.getMapper(OrderMapper.class);
            orderMapper.batchDeleteOrderItems(list);
            session.commit();
        }
    }

    @Test
    public void getListTest() {
        try (SqlSession session = sqlSessionFactory.openSession(false)) {
            OrderMapper orderMapper = session.getMapper(OrderMapper.class);
            Order order = orderMapper.selectOrderById(1);
            System.out.println("订单编号:" + order.getId() + "订单总价:" + order.getPayment());
            System.out.println("订单明细：");
            order.getOrderItems()
                    .forEach(orderItem -> System.out.println("商品名称：" + orderItem.getGoodsName() + ",商品金额："
                            + orderItem.getGoodsPrice() + ",购买个数：" + orderItem.getCount()));
            System.out.println("===================================================");
        }
    }
}
